package cn.talkimages.web.admin.actions.keyword;

import cn.talkimages.framework.utils.string.StringUtil;
import cn.talkimages.web.admin.constant.AdminConstDef;
import cn.talkimages.web.admin.constant.MessageKeys;
import cn.talkimages.web.beans.AdminBean;
import cn.talkimages.web.beans.KeywordBean;

public class KeywordUpdateAction extends KeywordAction {

	private static final long serialVersionUID = -971599906463248840L;

	private String updateType;

	// ----------------------- Form Properties ---------------------- //

	/** 编号 */
	private String id;

	/** 关键字中文名称 */
	private String nameZh;

	/** 关键字英文名称 */
	private String nameEn;

	// ---------------------------- 更新 ---------------------------- //

	/**
	 * 关键字 新建/更新
	 * 
	 * @throws Exception
	 */
	public String update() throws Exception {

		String result = ERROR;

		if (StringUtil.isBlank(id)) {
			updateType = AdminConstDef.UpdateType.Add;

			result = add();

		} else {
			updateType = AdminConstDef.UpdateType.Edit;

			result = edit();
		}

		return result;
	}

	// ---------------------------- 创建 ---------------------------- //

	/**
	 * 新建关键字
	 * 
	 * @throws Exception
	 */
	public String add() throws Exception {

		return SUCCESS;
	}

	// ---------------------------- 编辑 ---------------------------- //

	/**
	 * 编辑关键字
	 * 
	 * @throws Exception
	 */
	public String edit() throws Exception {

		if (!StringUtil.isNumeric(id)) {
			return NOTFOUND;
		}

		KeywordBean keyword = keywordService.getKeywordById(Long.parseLong(id));

		unpackageData(keyword);

		return SUCCESS;
	}

	// ---------------------------- 提交 ---------------------------- //

	/**
	 * 保存关键字
	 * 
	 * @throws Exception
	 */
	public String save() throws Exception {

		// 检查入力数据有效性
		if (!validateInputData()) {
			return INPUT;
		}

		// gather data from form
		KeywordBean keywordBean = packageData();

		if (AdminConstDef.UpdateType.Add.equals(updateType)) {
			keywordService.insertKeyword(keywordBean);
		} else {
			keywordService.updateKeywordById(keywordBean);
		}

		return SUCCESS;
	}

	/**
	 * 检查入力数据有效性
	 * 
	 * @return boolean 有效:true 有错误数据:false
	 */
	protected boolean validateInputData() {

		if (StringUtil.isBlank(nameZh)) {
			addActionError(getText(MessageKeys.Common.FIELD_REQUIRED,
					new String[] { "关键字中文名称" }));
		} else if (nameZh.length() > 50) {
			addActionError(getText(MessageKeys.Common.FIELD_MAX_LENGTH,
					new String[] { "关键字中文名称", "50" }));
		}

		return getActionErrors().size() == 0;
	}

	/**
	 * 收集表单数据
	 * 
	 * @return KnowledgeBean
	 */
	protected KeywordBean packageData() {

		AdminBean loginUser = getLoginUser();
		KeywordBean keywordBean = new KeywordBean();

		if (StringUtil.isNumeric(id))
			keywordBean.setId(Long.parseLong(id));

		keywordBean.setNameZh(nameZh);
		keywordBean.setNameEn(nameEn);
		keywordBean.setCreateId(loginUser.getLoginId());
		keywordBean.setUpdateId(loginUser.getLoginId());

		return keywordBean;
	}

	/**
	 * 填充表单数据
	 */
	protected void unpackageData(KeywordBean keyword) {

		id = String.valueOf(keyword.getId());
		nameZh = keyword.getNameZh();
		nameEn = keyword.getNameEn();

	};

	// ---------------------------- 删除 ---------------------------- //

	/**
	 * 删除关键字
	 * 
	 * @throws Exception
	 */
	protected String delete() throws Exception {

		updateType = AdminConstDef.UpdateType.Delete;

		return SUCCESS;
	}

	// ----------------------- Getter & Setter ----------------------- //

	public String getUpdateType() {
		return updateType;
	}

	public void setUpdateType(String updateType) {
		this.updateType = updateType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNameZh() {
		return nameZh;
	}

	public void setNameZh(String nameZh) {
		this.nameZh = nameZh;
	}

	public String getNameEn() {
		return nameEn;
	}

	public void setNameEn(String nameEn) {
		this.nameEn = nameEn;
	}

}
